'''
case Type : 硬件故障类--内存
case Name ：共享内存不足，启动数据库
Create At: 2020-10-22
owner : n@ningyali
description:1、停止数据库
            2、打开postgresql.conf，设置shared_buffers_default值为1024G
            3、启动数据库
            4、恢复shared_buffers_default
            5、启动数据库
expect:1、停止数据库成功
        2、shared_buffers_default更新成功
        3、数据库启动失败，报错合理
        4、shared_buffers_default更新成功
        5、启动数据库成功
history：
    created： 2020/10/22  created by n@ningyali
'''
import unittest
from yat.test import Node
from yat.test import macro

from testcase.utils.CommonSH import *
from testcase.utils.Common import *


class SharedBufferShortAndStartDB(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('-----------Opengauss_Reliability_Hardware_Case002:开始执行------------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()

        self.log.info("---------------获取shared_buffers_default默认值----------------")
        sql_cmd = "show shared_buffers;"
        self.log.info(sql_cmd)
        msg = self.primary_sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.shared_buffers = msg.splitlines()[-2].strip()

    def test_main(self):
        self.log.info("---------------停止数据库----------------")
        is_stopped = self.primary_sh.stopDbCluster()
        self.assertTrue(is_stopped)

        self.log.info("---------------打开postgresql.conf，设置shared_buffers_default值为1024G----------------")
        msg = self.primary_sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'shared_buffers=1024GB')
        self.assertTrue(msg)

        self.log.info("-------------启动数据库---------------")
        is_started = self.primary_sh.startDbCluster(get_detail=True)
        self.log.info(is_started)
        self.assertIn(self.Constant.CANNOT_ALLOCATE_MEMORY_MSG, is_started)

        self.log.info("---------------恢复shared_buffers_default----------------")
        msg = self.primary_sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG,
                                           f'shared_buffers={self.shared_buffers}')
        self.assertTrue(msg)

        self.log.info("-------------启动数据库---------------")
        is_started = self.primary_sh.startDbCluster()
        self.assertTrue(is_started)

        self.log.info("-------------查看集群状态---------------")
        db_status = self.primary_sh.getDbClusterStatus('status')
        self.assertTrue(db_status)

    def tearDown(self):
        self.log.info("---------------恢复shared_buffers_default----------------")
        msg = self.primary_sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG,
                                           f'shared_buffers={self.shared_buffers}')
        self.assertTrue(msg)
        self.log.info('-------Opengauss_Reliability_Hardware_Case002:执行完成----------')
